//
//  ChildrenViewController.m
//  MM
//
//  Created by 王晨辉 on 15/5/15.
//  Copyright (c) 2015年 王晨辉. All rights reserved.
//

#import "ChildrenViewController.h"
#import "ChildrenTwoViewController.h"

@interface ChildrenViewController ()

@end

@implementation ChildrenViewController


-(void)viewWillAppear:(BOOL)animated{
    
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    report = [NSMutableArray array];
    reportId = [NSMutableArray array];

    
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    NSString *babyID = [user1 objectForKey:@"babyID"];

    
    NSDictionary *param = @{@"babyId": [BabyManager shareInstance].babyId};
    
    [UserDAL getBabyGrewUpReportParams:param withBlock:^(NSDictionary *data, NSError *error) {
        
        
        ////NSLog(@"%@",data);
        NSArray *myArray = data[@"dataList"];

        if (myArray.count!=0) {
            
            ////NSLog(@"11111");
            for (NSDictionary * dic in myArray) {
                
                NSString * reports = [dic objectForKey:@"report"];
                NSString * reportIds = [dic objectForKey:@"reportId"];

                [reportId addObject:reportIds];
                [report addObject:reports];

                
            }
            
            [_tableView reloadData];

        }else{
            
            alert(@"请先做成长测评，稍后出测评报告");


            ////NSLog(@"22222");

        }
        
        
        
    }];
    
    
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 52;
    _tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView];

    //设置导航栏头文字
    self.navigationItem.title = @"园内报告";
    //设置导航栏背景颜色
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#44afd7" alpha:1]];
    //设置导航栏字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //自定义左右按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(turntolast)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 20, 15);
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    self.navigationItem.leftBarButtonItem = menuButton;
    
    
    
}
-(void)turntolast{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}
#pragma mark--- tableViewDelegate 每个区的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return reportId.count;
}

#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
    }
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    [[cell textLabel]  setText:[report objectAtIndex:indexPath.row]];
    _tableView.separatorStyle = UITableViewCellEditingStyleNone;
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, _tableView.rowHeight-1, SCREEN_WIDTH, 1)];
    view.backgroundColor = UIColorWithRGBA(229, 229, 231, 1);
    [cell addSubview:view];
    
    return cell;
    
    
}
#pragma mark--- tableViewDelegate 有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
    
}
#pragma mark--- tableViewDelegat 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ChildrenTwoViewController * childTow = [[ChildrenTwoViewController alloc]init];
    childTow.reportId = reportId[indexPath.row];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:childTow];
    
    
    [self presentViewController:nav animated:YES completion:nil];
    
    
}
    // Do any additional setup after loading the view from its nib.

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
